package com.CampusLife.controller;

import com.CampusLife.bean.*;
import com.CampusLife.service.*;
import com.CampusLife.until.DeleteImages;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import  com.CampusLife.until.DataTime;
import  com.CampusLife.until.ImagesUpload;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/product")
public class ProductController {
@Resource
    private IProductService iProductService;
@Resource
 private IProductTypeService iProductTypeService;
@Resource
private IPictureService iPictureService;
@Resource
private IProductCommentService iProductCommentService;
@Resource
private IFavoriteService iFavoriteService;
    @RequestMapping("/query")
    public  String  queryAll(Model model,@RequestParam(defaultValue="1",required=true,value="pageNo") Integer pageNo){
        //每页显示记录数
        Integer pageSize=15;
        PageHelper.startPage(pageNo, pageSize);
        List<Product> list= iProductService.queryAll();
        PageInfo<Product> pageInfo=new PageInfo<Product>(list);
        model.addAttribute("pageInfo",pageInfo);
        List<ProductType> Type=iProductTypeService.queryAll();
        model.addAttribute("Type",Type);
        int sort=1;
        model.addAttribute("sort",sort);
        return  "home/shopList";
    }
    @RequestMapping("/queryByName")
    public  String  queryByName(Model model,String p_name,@RequestParam(defaultValue="1",required=true,value="pageNo") Integer pageNo){
        //每页显示记录数
        Integer pageSize=15;
        PageHelper.startPage(pageNo, pageSize);
        List<Product> list= iProductService.queryByName(p_name);
        PageInfo<Product> pageInfo=new PageInfo<Product>(list);
        model.addAttribute("pageInfo",pageInfo);
        List<ProductType> Type=iProductTypeService.queryAll();
        model.addAttribute("Type",Type);
        int sort=2;
        model.addAttribute("sort",sort);
        model.addAttribute("p_name",p_name);
        return  "home/shopList";
    }
     @RequestMapping("/queryAll")
public  String  findall(Model  model){
    List<Product> products=iProductService.getProductAll();
    model.addAttribute("products",products);
    List<ProductType> list=iProductTypeService.queryAll();
    model.addAttribute("list",list);
    return  "admin/shopbasic";
}
 @RequestMapping("/getProductAllByName")
 public String getProductAllByName(String p_name,Model model){
    Product product=new Product();
     product.setP_name(p_name);
     List<Product> products=iProductService.getProductAllByName(product);
     model.addAttribute("products",products);
     List<ProductType> list=iProductTypeService.queryAll();
     model.addAttribute("list",list);
     return  "admin/shopbasic";
 }
    @RequestMapping("/findAllShen")
    public  String  findallshen(Model  model){
        List<Product> products=iProductService.getProductAllShen();
        model.addAttribute("products",products);
        List<ProductType> list=iProductTypeService.queryAll();
        model.addAttribute("list",list);
        return  "admin/shenhe";
    }
@RequestMapping("getProductByType")
public String getProductByType(Integer t_id,Model model){
    List <Product> products=iProductService.getProductByType(t_id);
    model.addAttribute("products",products);
    List<ProductType> list=iProductTypeService.queryAll();
    model.addAttribute("list",list);
    return  "admin/shopbasic";
}
    @RequestMapping("getProductByType1")
    public String getProductByType1(Integer t_id,Model model,@RequestParam(defaultValue="1",required=true,value="pageNo") Integer pageNo){
        Integer pageSize=15;
        PageHelper.startPage(pageNo, pageSize);
        List <Product> list=iProductService.getProductByType(t_id);
        PageInfo<Product> pageInfo=new PageInfo<Product>(list);
        model.addAttribute("pageInfo",pageInfo);
        List<ProductType> type=iProductTypeService.queryAll();
        model.addAttribute("Type",type);
        int sort=3;
        model.addAttribute("sort",sort);
        model.addAttribute("t_id",t_id);
        return  "home/shopList";
    }
    @RequestMapping("inAddJsp")
    public String inAddJsping(Model model){
        List<ProductType> list=iProductTypeService.queryAll();
        model.addAttribute("list",list);
        return  "home/addshop";
    }

@RequestMapping(path = "/addShop", method = RequestMethod.POST)
@ResponseBody
public int addUser(@RequestParam("file") MultipartFile file, HttpServletRequest request,
                          HttpServletResponse response,HttpSession httpSession, ModelMap model,Float price,String phone,String describe_txt,Integer t_id,String p_name) throws Exception {
        //如果该文件内容为空，返回501，弹出对话框“商品图片上传出现问题，请重新上传”
        if (file.isEmpty()) {
                return -500;
              }
      else if (!file.isEmpty()){ ImagesUpload imagesUpload =new ImagesUpload();
      String photo=  imagesUpload.Upload(file);
            Product product=new Product();
            product.setT_id(t_id);
            product.setPrice(price);
            product.setDescribe_txt(describe_txt);
            product.setPhone(phone);
            product.setP_name(p_name);
            product.setStatus(0);
            product.setPhoto(photo);
          DataTime dataTime=new DataTime();
            product.setTime(dataTime.gettime());
            int i=(int)httpSession.getAttribute("id");
            product.setId(i);
            iProductService.add(product);
          //添加成功
           return 0;}
      else {
          return 1;
        }
    }
    //跳转到修改页面
    @RequestMapping("/jumpUpdateProductEdit")
    public String jumpUpdateProductEdit(Integer pid,Model model){
        List<ProductType> list=iProductTypeService.queryAll();
        model.addAttribute("list",list);
       Product product=iProductService.queryById(pid);
       model.addAttribute("product",product);

        return "home/updateshop";
    }
    //修改  /图片上传
    @RequestMapping("/ToUpdateProduct")
    @ResponseBody
    public int updateProduct(@RequestParam("file") MultipartFile file, HttpServletRequest request,
                                HttpServletResponse response,HttpSession httpSession, ModelMap model,int pid,Float price,String phone,String describe_txt,Integer t_id,String p_name) throws Exception {

        if (!file.isEmpty()) {
            ImagesUpload imagesUpload = new ImagesUpload();
            String photo = imagesUpload.Upload(file);
            Product product = new Product();
            product.setPid(pid);
            product.setT_id(t_id);
            product.setPrice(price);
            product.setDescribe_txt(describe_txt);
            product.setPhone(phone);
            product.setStatus(0);
            product.setP_name(p_name);
            product.setPhoto(photo);
            DataTime dataTime = new DataTime();
            product.setTime(dataTime.gettime());
            int i = (int) httpSession.getAttribute("id");
            product.setId(i);
            iProductService.update(product);
            //修改成功
            return 0;
        }    else if (file.isEmpty()) {
            return -500;
        }
        else {
            return 1;
        }
    }
    @RequestMapping("/delete")
    public String delete( Integer pid)throws IOException {
      Product product= iProductService.getProductById(pid);
        DeleteImages deleteImages=new DeleteImages();
        File url= new File(product.getPhoto());
        deleteImages.deleteDirectory(url);
      iProductService.delete(pid);
        return "redirect:/product/queryAll";
    }

    @RequestMapping("/userBydelete")
    public String userBydelete( Integer pid){

        iProductService.delete(pid);
        return "redirect:/product/getProductByUserId";
    }
    @RequestMapping("/getProductAllByName0")
    public String getProductAllByName0(String p_name,Model model){
        Product product=new Product();
        product.setP_name(p_name);
        List<Product> products=iProductService.getProductAllByName0(product);
        model.addAttribute("products",products);
        List<ProductType> list=iProductTypeService.queryAll();
        model.addAttribute("list",list);
        return  "admin/shopbasic";
}
    @RequestMapping("getProductByType0")
    public String getProductByType0(Integer t_id,Model model) {
        List<Product> products = iProductService.getProductByType0(t_id);
        model.addAttribute("products", products);
        List<ProductType> list = iProductTypeService.queryAll();
        model.addAttribute("list", list);
        return "admin/shopbasic";
    }

    @RequestMapping("/upadteStatus")
    public  String updateStatus(Integer pid){

        iProductService.updateStatus(pid);
        return "redirect:/product/findAllShen";
    }

    /**
     * 展示商品详情及该商品留言
     * @param pid
     * @param model
     * @return
     */
    @RequestMapping("/getProductById")
    public  String  getProductById( Model model, HttpSession httpSession, int pid){


    Product product= iProductService.getProductById(pid);
    model.addAttribute("product", product);
    List<ProductComment> productComments= iProductCommentService.queryById(pid);

    model.addAttribute("productComments", productComments);
        int userId=(int)httpSession.getAttribute("id");
        model.addAttribute("userId",userId);
        return "home/xiangqing";
    }
    @RequestMapping("/getProductByUserId")
    public  String getProductByUserId(Model model,HttpSession httpSession){
        int userId=(Integer) httpSession.getAttribute("id");
        List<Product> products=iProductService.getProductByUserId(userId);
        model.addAttribute("products",products);
        return "home/user-shop";
    }
    @RequestMapping("/toShop")
    public  String   toShop(Model model,HttpSession httpSession){
        List<Product>  book = iProductService.getProductByBook();
        model.addAttribute("book", book);
        List<Product>  Computer = iProductService.getProductByComputer();
        model.addAttribute("Computer",Computer);
        List<Product>  Phone = iProductService.getProductByPhone();
        model.addAttribute("Phone", Phone);
        List<Product>  Electric= iProductService.getProductByElectric();
        model.addAttribute("Electric", Electric);
        List<Product> Equipment = iProductService.getProductByEquipment();
        model.addAttribute("Equipment",Equipment);
        List<Product> Vehicle = iProductService.getProductByVehicle();
        model.addAttribute("Vehicle", Vehicle);
        List<Product> life = iProductService.getProductByLife();
        model.addAttribute("life", life);
        List<Picture> pictures=iPictureService.getShop();
        model.addAttribute("pictures",pictures);
        List<ProductType> ProductTypes=iProductTypeService.queryATop();
        model.addAttribute("ProductTypes",ProductTypes);
        List<ProductType> ProductType=iProductTypeService.queryEnd();
        model.addAttribute("ProductType",ProductType);
        Integer id=(Integer)httpSession.getAttribute("id");
        int count=iFavoriteService.queryCount(id);
        model.addAttribute("count",count);
       return "home/ShopIndex";
    }


}